﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FDRemoteAssistant2
{
    /*
     * CreatedOn: 2018-05-16
     * CreatedBy: Jackie Lee(天宇遊龍)
     */
    /// <summary>
    /// 分页控件
    /// </summary>
    public partial class DataPagercs : UserControl
    {
        private object _data;
        private int _pageIndex, _pageSize, _total, _pageCount;

        public event EventHandler NavToPre, NavToNext, NavToFirst, NavToLast;

        public int PageIndex => _pageIndex;

        #region 初始化
        public DataPagercs()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            btnFirst = new Button();
            btnPrev = new Button();
            btnNext = new Button();
            btnLast = new Button();
            lblCount = new Label();

            int left = 5, top = 5, padding = 5;
            AddControl(btnFirst, "首页", 75, 30, left, top);
            left += btnFirst.Width + padding;
            AddControl(btnPrev, "上一页", 75, 30, left, top);
            left += btnPrev.Width + padding;
            AddControl(btnNext, "下一页", 75, 30, left, top);
            left += btnNext.Width + padding;
            AddControl(btnLast, "末页", 75, 30, left, top);
            left += btnLast.Width + padding;
            AddControl(lblCount, "", 85, 30, left, top + 10);
            ShowInfo();

            MinimumSize = new Size(left + lblCount.Width, btnLast.Height + 10);
            Size = MinimumSize;

            btnFirst.Click += BtnFirst_Click;
            btnPrev.Click += BtnPrev_Click;
            btnNext.Click += BtnNext_Click;
            btnLast.Click += BtnLast_Click;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public void InitData<T>(IList<T> data, int pageIndex, int pageSize)
        {
            _data = data;
            _pageIndex = pageIndex;
            _pageSize = pageSize;
            _total = data.Count;
            _pageCount = data.Count / _pageSize + (data.Count % _pageSize == 0 ? 0 : 1);
            CheckStatus();
        }
        
        #endregion
        
        #region 事件处理

        #region 按钮事件
        private void BtnFirst_Click(object sender, EventArgs e)
        {
            if (_pageIndex > 1)
            {
                _pageIndex = 1;
            }
            CheckStatus();
            NavToFirst?.Invoke(this, e);
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            if (_pageIndex > 1 && _pageCount > 1)
            {
                _pageIndex--;
            }
            CheckStatus();
            NavToPre?.Invoke(this, e);
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            if (_pageIndex < _pageCount)
            {
                _pageIndex++;
            }
            CheckStatus();
            NavToNext?.Invoke(this, e);
        }

        private void BtnLast_Click(object sender, EventArgs e)
        {
            if (_pageIndex < _pageCount)
            {
                _pageIndex = _pageCount;
            }
            CheckStatus();
            NavToLast?.Invoke(this, e);
        }

        #endregion

        #endregion

        #region 逻辑处理
        private void ShowInfo()
        {
            lblCount.Text = $"{_pageIndex}/{_pageCount} (共{_total}条)";
        }

        private void CheckStatus()
        {
            btnFirst.Enabled = btnPrev.Enabled = btnNext.Enabled = btnLast.Enabled = true;
            if (_pageIndex == 1)
            {
                btnFirst.Enabled = btnPrev.Enabled = false;
            }
            if (_pageIndex == _pageCount || _pageCount == 0)
            {
                btnNext.Enabled = btnLast.Enabled = false;
            }
            ShowInfo();
        }

        private void AddControl(Control ctl, string text, int width, int height, int left, int top)
        {
            ctl.Width = width;
            ctl.Height = height;
            ctl.Left = left;
            ctl.Top = top;
            ctl.Text = text;
            Controls.Add(ctl);
        }

        /// <summary>
        /// 获取结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IList<T> GetResult<T>()
        {
            if (_data == null)
                return null;
            IList<T> data = (IList<T>)_data;
            return data.Skip((_pageIndex - 1) * _pageSize).Take(_pageSize).ToList();
        }
        #endregion

        #region 释放
        
        #endregion
    }
}
